﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Globalization;
using System.Threading;

namespace ProjectStudy.App_Code
{
    public class BasePage : System.Web.UI.Page
    {
        public bool hasLogin = false;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (hasLogin)
            {
                Session.Clear();
                Session.Abandon();
                Response.Write("<script language=javascript>alert('帐号已在别处登陆，你将被强迫下线（请保管好自己的用户密码）！');</script>");
            }

            if (IsLogin())
            {
                if (!IsMySite())
                {
                    Response.Write("Error!");
                    Response.End();
                }
            }
            else
            {
                Response.Write("<script> top.location.href='../注册登录找回密码修改密码页面/Logout.aspx';</script>");
                Response.End();
            }          
        }

        /// <summary>
        /// 判断用户是否已登录，如果已登录，则将旧的登录用户强制下线
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreInit(EventArgs e)
        {
            Hashtable hOnline = (Hashtable)Application["Online"];
            if (hOnline != null)
            {
                IDictionaryEnumerator idE = hOnline.GetEnumerator();
                while (idE.MoveNext())
                {
                    if (idE.Key != null && idE.Key.ToString().Equals(Session.SessionID))
                    {
                        if (idE.Value != null && "XXXXXX".Equals(idE.Value.ToString()))
                        {
                            hOnline.Remove(Session.SessionID);
                            Application.Lock();
                            Application["Online"] = hOnline;
                            Application.UnLock();

                            hasLogin = true;
                            return;
                        }
                        break;
                    }
                }
            }
        }

        public BasePage()
        { 
          
        }

        protected bool IsLogin()
        {         
            if (Session["username"] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        protected bool IsMySite()
        {
            bool isMySite = false;
            string HTTP_REFERER = Request.ServerVariables["HTTP_REFERER"];
            
            if (HTTP_REFERER != null)
            {
                string SERVER_NAME = Request.ServerVariables["SERVER_NAME"];
                int i = HTTP_REFERER.IndexOf("://");
                if (i >= 0)
                {
                    if (HTTP_REFERER.Substring(i + 3, SERVER_NAME.Length) == SERVER_NAME)
                    {
                        isMySite = true;
                    }
                }
            }
            return isMySite;
        }

        protected override void InitializeCulture()
        {
            base.InitializeCulture();

            HttpCookie cookie = new HttpCookie("USER_COOKIE");
            string lang = "";
            if (Request.QueryString["lang"] != null)
            {
                lang = Request.QueryString["lang"];
                cookie.Values.Add("lang", Request.QueryString["lang"]);

                Response.Cookies.Add(cookie);

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            }
            else if (Request.Cookies["USER_COOKIE"] != null)
            {
                cookie = Request.Cookies["USER_COOKIE"];
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie["lang"]);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie["lang"]);
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-CN");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
            }

        }

        
    }
}